package com.doubledr.light_lock.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.doubledr.light_lock.R;

import java.util.List;
import java.util.Map;

public class MenuAdapter extends BaseAdapter {
    private Context context = null;
    private List<Map<String, Object>> datalist;

    public MenuAdapter(Context context, List<Map<String, Object>> datalist){
        this.context = context;
        this.datalist = datalist;
    }
    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            mHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.menu_item, null, true);
            mHolder.menu_img = convertView.findViewById(R.id.menuImg);
            mHolder.menu_title =  convertView.findViewById(R.id.menuText);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        String title = datalist.get(position).get("title").toString();
        String img = datalist.get(position).get("img").toString();

        int picId = Integer.parseInt(img);
        mHolder.menu_img.setImageResource(picId);
        mHolder.menu_title.setText(title);
        return convertView;
    }
    class ViewHolder {
        private ImageView menu_img;
        private TextView  menu_title;
    }
}
